import QtQuick 2.15
import QtQuick.Controls 2.15

SubView1 {
    id: root
    width: 532
    height: 349

    // 新增：存储重量变化检测相关属性
    property real lastWeightValue: 0
    property real weightChangeThreshold: 100  // 重量变化阈值

    AdditionalLightBar  //一行文字：不稳   正常    非零    正号
    {
        id:additionalLight
    }

    signal stopTimer;
    signal startTimer;
    onStopTimer: {
        collectWeightTimer.running=false
    }
    onStartTimer: {
        collectWeightTimer.running=true
    }
    Component.onDestruction: {
        collectWeightTimer.destroy()    //QQ 为什么需要destroy()？
    }
    Component.onCompleted: {
        //cmbUnit.currentIndex = cmbUnit.find(mymodbus.unit);
        mymodbus.getDecimalPlaces()
        mymodbus.getMaxRange()
    }
    Rectangle
    {
        id:displayPanel         //哦哦，白色边框。
        x: 1
        y: 36                   //哦哦，固定位置的清零按钮和"列表框"。
        width: 531
        height: 312
        color:"transparent"
        border.color: "white"
        Column  //哦 列。
        {
            x: 423
            y: 10
            spacing: 5
            Button
            {
                id:btnSetZero
                width: 100
                height: 50
                background: Rectangle{
                    border.width: 0
                    color: "#7584C2"
                        }

                text: qsTr("清零")
                font.pixelSize: 25
                palette.buttonText: "white"
                onClicked: {
                    //mymodbus.setZeroWeight1(mymodbus.weightNew)
                    mymodbus.slotZeroWeight();
                    // 清零时重置重量变化检测
                    lastWeightValue = 0;
                }
            }

            //MyComboBox
            //{
            //    id:cmbUnit
            //    width: 100
            //    height: 45
            //    borderColor:"white"
            //    fillColor:"#0A3653"
            //    model:["ug","mg","g","kg","Mg","t","lb","dyne","kdyne","oz","TONNE"]
            //    //currentIndex: cmbUnit.find(mymodbus.unit)
            //    onActivated: {
            //        mymodbus.setUnit(cmbUnit.currentText)
            //    }
            //}
        }

        Text
                {
                    id: weight
                    //text: mymodbus.weight+mymodbus.unit
                        //qsTr("999999")
                    color: "white"
                    font.pixelSize: 80
                    width:parent.width
                    height:parent.height
                    horizontalAlignment: Text.AlignHCenter
                    verticalAlignment: Text.AlignVCenter
                }
//        Text
//        {
//            id:unit
//            x: 421
//            y: 124
//            text:qsTr("g")
//            color: "white"
//            font.pixelSize: 50
//        }


    }

    Timer{
        id: collectWeightTimer
        running: true
        repeat: true
        interval: mymodbus.collectTime
        onTriggered: {
            weight.text = mymodbus.weightNew+mymodbus.unit  
            // 动态计算重量变化阈值：maxRange除以10的decimalPlaces次方的千分之2
            var maxRangeValue = mymodbus.maxRange;  // 默认值100000
            var decimalPlacesValue = mymodbus.decimalPlaces;  // 默认值0
            weightChangeThreshold = (maxRangeValue / Math.pow(10, decimalPlacesValue)) * 0.002;
            
            // 重量变化检测逻辑
            var newWeight = parseFloat(mymodbus.weightNew);
            
            // 计算重量变化值
            var weightChange = Math.abs(newWeight - lastWeightValue);
            
            // 如果重量变化超过阈值，重置屏保计时器
            if (weightChange > weightChangeThreshold) {
                //console.log("重量变化超过阈值:", weightChange, ">", weightChangeThreshold);
                // 调用main.qml中的resetScreenSaverTimer函数
                if (typeof rectangle !== 'undefined' && typeof rectangle.resetScreenSaverTimer === 'function') {
                    rectangle.resetScreenSaverTimer();
                    //console.log("重量变化检测到，已重置屏保计时器");
                } else {
                    //console.log("警告：无法找到resetScreenSaverTimer函数");
                }                
            }
            // 更新上次重量值
            lastWeightValue = newWeight;
        }
    }
}

